package com.example.administrator.yuejian35.Login;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.yuejian35.Login.activity.RegActivity;
import com.example.administrator.yuejian35.Login.bean.LoginResult;
import com.example.administrator.yuejian35.Login.model.LoginModel;
import com.example.administrator.yuejian35.MainActivity;
import com.example.administrator.yuejian35.R;
import com.example.administrator.yuejian35.base.BaseFragment;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static android.R.attr.mode;
import static android.app.Activity.RESULT_OK;

/**
 * A simple {@link Fragment} subclass.
 */
public class LoginFragment extends BaseFragment {
    private EditText et_userName;
    private EditText et_password;
    private Button btn_login;
    private TextView btn_reg;
    private String userName;
    private String password;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private View view;

    public LoginFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        view = inflater.inflate(R.layout.fragment_login, container, false);
        et_userName = (EditText) view.findViewById(R.id.editText_uname);
        et_password = (EditText) view.findViewById(R.id.editText_password);
        btn_login = (Button) view.findViewById(R.id.button);
        btn_reg = (TextView) view.findViewById(R.id.toreg);



        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), RegActivity.class);
                startActivityForResult(intent,1);

            }
        });

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                userName = et_userName.getText().toString();
                password = et_password.getText().toString();

                Log.v("name",userName);
                Log.v("pass",password);

                LoginModel loginmodel = new LoginModel();
                Call call = loginmodel.DoLogin(userName,password);
                call.enqueue(new Callback<LoginResult>() {
                    @Override
                    public void onResponse(Call<LoginResult> call, Response<LoginResult> response) {


                        String UserId = response.body().getUid().toString();
                        String uname = response.body().getUname().toString();

                        if(UserId.equals("false")){
                            Toast.makeText(getActivity(),"用户名或密码错误", Toast.LENGTH_SHORT).show();
                        }
                        else{
                            //显示主界面
                            Toast.makeText(getActivity(),"登录成功"+" "+UserId, Toast.LENGTH_SHORT).show();

                            sp = getActivity().getSharedPreferences("User", Context.MODE_PRIVATE);
                            editor=sp.edit();
                            editor.putString("user_id",UserId);
                            editor.commit();

                            MainActivity mainActivity = (MainActivity) getActivity();
                            if(mainActivity!=null){
                                mainActivity.replaceLoginFragment();
                                setUserId(UserId);
                                setUserName(uname);
                            }
                        }
                        //Toast.makeText(getActivity(),"welcome", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailure(Call<LoginResult> call, Throwable t) {

                    }
                });
            }
        });



        return view;
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);  //data是返回的数据

        switch (requestCode){
            case 1:
                switch (resultCode){
                    case RESULT_OK:
                        String un = data.getStringExtra("name");
                        et_userName.setText(un);
                        break;
                }

                break;
            default:
                break;
        }



    }

}
